package com.ehais.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ehais.modbuswifi.MyApplication;
import com.ehais.modbuswifi.R;
import com.ehais.model.NewScanBatteryData;

import java.util.List;

public class NewScanBatteryAdapter extends ArrayAdapter<NewScanBatteryData> {
    private Context context;
    public NewScanBatteryAdapter(@NonNull Context context, int resource, @NonNull List<NewScanBatteryData> objects) {
        super(context, resource, objects);
        this.context = context;
    }

    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        NewScanBatteryData model = getItem(position);//得到当前项的 Fruit 实例
        View view;
        //新增一个内部类 ViewHolder，用于对控件的实例进行缓存
        ViewHolder viewHolder;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_new_scan_battery,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvVoltageText = view.findViewById(R.id.tvVoltageText);
            viewHolder.tvVoltageValue = view.findViewById(R.id.tvVoltageValue);
            viewHolder.tvVoltageUnit = view.findViewById(R.id.tvVoltageUnit);
            viewHolder.tvElectricityText = view.findViewById(R.id.tvElectricityText);
            viewHolder.tvElectricityValue = view.findViewById(R.id.tvElectricityValue);
            viewHolder.tvElectricityUnit = view.findViewById(R.id.tvElectricityUnit);
            viewHolder.gvMonomerBattery = view.findViewById(R.id.gvMonomerBattery);
            view.setTag(viewHolder);//将 viewHolder 存储在 view 中
        }else{
            view=convertView;
            viewHolder= (ViewHolder) view.getTag();//重新获取 viewHolder
        }
        viewHolder.tvVoltageValue.setText(model.getVoltage().toString());
        viewHolder.tvElectricityValue.setText(model.getCurrent().toString());
        if(model.getAlarmTotalVoltage() == true){//总电压告警
            viewHolder.tvVoltageText.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
            viewHolder.tvVoltageValue.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
            viewHolder.tvVoltageUnit.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
        }else{
            viewHolder.tvVoltageText.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
            viewHolder.tvVoltageValue.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
            viewHolder.tvVoltageUnit.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
        }

        if(model.getAlarmElectricity() == true){//电流告警
            viewHolder.tvElectricityText.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
            viewHolder.tvElectricityValue.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
            viewHolder.tvElectricityUnit.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.red));
        }else{
            viewHolder.tvElectricityText.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
            viewHolder.tvElectricityValue.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
            viewHolder.tvElectricityUnit.setTextColor(MyApplication.getInstance().getResources().getColor(R.color.purple));
        }


        NewScanBatteryMonomerAdapter adapter = new NewScanBatteryMonomerAdapter(this.context,R.layout.adapter_new_scan_battery_monomer,model.getMonomerVoltageList());
        viewHolder.gvMonomerBattery.setAdapter(adapter);
        ViewGroup.LayoutParams params = viewHolder.gvMonomerBattery.getLayoutParams();

        int numColumns = viewHolder.gvMonomerBattery.getNumColumns();
        if(numColumns > 0){
            int totalHeight = 0;
            int itemCount = adapter.getCount();
            for(int i = 0; i < itemCount; i += numColumns) {
                View listItem = adapter.getView(i,null,viewHolder.gvMonomerBattery);
                listItem.measure(0,0);
                totalHeight += listItem.getMeasuredHeight();
            }
            params.height = totalHeight + (viewHolder.gvMonomerBattery.getVerticalSpacing() * (itemCount / numColumns - 1));

            viewHolder.gvMonomerBattery.setLayoutParams(params);
            viewHolder.gvMonomerBattery.requestLayout();
        }


        return view;
    }

    private class ViewHolder {
        TextView tvVoltageText;
        TextView tvVoltageValue;
        TextView tvVoltageUnit;
        TextView tvElectricityText;
        TextView tvElectricityValue;
        TextView tvElectricityUnit;
        GridView gvMonomerBattery;
    }

}
